package com.ytf.file.local;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author YTF
 * @className FileStorageConfig
 * @date 2025/5/31 17:03
 * @description 文件存储配置类
 */
@Configuration
public class FileStorageConfig implements WebMvcConfigurer {

    @Value("${file.upload-dir}")
    private String uploadDir;

    public Path getUploadPath() {
        return Paths.get(uploadDir).toAbsolutePath().normalize();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射上传目录到静态资源
        String uploadLocation = "file:///" + getUploadPath().toString().replace('\\', '/') + '/';
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations(uploadLocation);
    }
}